export default {

    namespace: 'example',

    state: {
        mapLines: [{
            id: 'line0',
            paths: [[75.757904,38.118117],[117.375719,24.598057],[115.375719,28.598057]],
        },{
            id: 'line1',
            paths: [[76.757904,38.118117],[119.375719,24.598057],[114.375719,28.598057]],
        }],
        mapPoints: [{
            id:'p00',
            longitude:116.468021,
            latitude:39.890092,
            markerContent: '<div style="background:#fff;">1111<div>',
            config: {
                markerContentX: -16,
                markerContentY: -9,
                isAnimation: false,
                animationDelay: 10,
                // BAnimationType: 0
            },
            other: 'xxxx'
        },{
            id:'p01',
            longitude:117.468021,
            latitude:39.890092,
            canShowLabel: true,
            config: {
                labelContent: 'asdfasdfasdfasfdasf',
                markerContentX: -13,
                markerContentY: -42,
                isAnimation: true,
                animationDelay: 30,
                // BAnimationType: 1
            }
        },{
            id:'p02',
            longitude:117.468021,
            latitude:39.890092,
            config: {
                markerContentX: -13,
                markerContentY: -42,
                isAnimation: true,
                animationDelay: 30,
                autoRotation: true
                // BAnimationType: 1
            }
        }],
        mapPolygons: [{
            id: 'g00',
            rings: [[118.757904,38.118117],[112.375719,24.598057],[115.375719,28.598057]]
        },{
            id: 'g01',
            rings: [[116.757904,36.118117],[115.375719,25.598057],[114.375719,38.598057]]
        }],
        mapCircles: [{
            id: 'c00',
            longitude: 111.98098,
            latitude: 38.0873234,
            radius: 30000
        },{
            id: 'c01',
            longitude: 119.98098,
            latitude: 32.0873234,
            radius: 100000
        }],
        heatmap: {
            data: [
                {"lng":121.173004,"lat":30.280188,"count":80},
                {"lng":120.473004,"lat":31.480188,"count":34},
                {"lng":122.373004,"lat":28.280188,"count":19},
                {"lng":123.173004,"lat":30.255088,"count":42},
                {"lng":119.173004,"lat":28.280188,"count":32},
                {"lng":117.173004,"lat":29.280188,"count":98},
                {"lng":120.223004,"lat":31.280188,"count":53},
                {"lng":120.453004,"lat":29.280188,"count":25},
                {"lng":118.173004,"lat":32.280188,"count":10}
            ],
            //lng 经度
            //lat 纬度
            //count 权重值
            config: {
                radius: 50,
                //热力图半径,默认20
                visible: true,
                //控制热力图显隐,默认true 
                max: 100,
                //最大权重值 默认100
                gradient: {
                    0:'rgb(102,255, 0)',
                    .5:'rgb(255,170,0)',
                    1:' #FF33FF'
                },
                //渐变区间 (ui没有定就不要设置该字段)
                opacity: 0.5,
                //透明度
            }
        },
        isOpenTrafficInfo: false,
        setVisiblePoints: false,
        mapVisiblePoints: '',
        setCenter: false,
        mapCenter: '',
        setZoomLevel: false,
        mapZoomLevel: '',
        mapCluster: '',
        setCluster: false,
        boundaryName: ['苏州'],
        isRangingTool: false,
        mapDraw: '',
        isDraw: false,
        isCloseDraw: false,
        isClearAll: false,
        mapRemove: [],
        isRemove: false,
        editGraphicId: '',
        isDoEdit: false,
        isEndEdit: false,
        isSetAreaRestriction: false,
        areaRestriction: [[115.345746,39.488275],[117.455121,40.322595]],
        isClearAreaRestriction: false,
        inputVal:''
    },
    subscriptions: {
        setup({ dispatch, history }) {
        },
    },

    effects: {
        //最优视距
        *visiblePoints({ payload }, { call, put , select}) {
            yield put({
                type: 'updateState',
                payload: {
                    setVisiblePoints: true,
                    mapVisiblePoints:{fitView:'line'}
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    setVisiblePoints: false
                }
            })
        },
        //设置中心点
        *setCenter({ payload }, { call, put , select}) {
            yield put({
                type: 'updateState',
                payload: {
                    setCenter: true,
                    mapCenter:[117.39388495,36.3938745759]
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    setCenter: false
                }
            })
        },
        //设置zoom等级
        *setZoomLevel({ payload }, { call, put , select}){
            let mapZoomLevel = Math.random() * 20;
            if(mapZoomLevel > 18){
                mapZoomLevel = 18;
            }else if(mapZoomLevel < 3){
                mapZoomLevel = 3;
            }
            yield put({
                type: 'updateState',
                payload: {
                    setZoomLevel: true,
                    mapZoomLevel: mapZoomLevel
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    setZoomLevel: false
                }
            })
        },
        //设置点聚合
        *setCluster({ payload }, { call, put , select}){
            yield put({
                type: 'updateState',
                payload: {
                    setCluster: true,
                    // mapCluster: ['p00','p01']
                    mapCluster: 'p00,p01'
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    setCluster: false
                }
            });
        },
        *editRangingTool({ payload }, { call, put , select}){
            yield put({
                type: 'updateState',
                payload: {
                    isRangingTool: true,
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isRangingTool: false
                }
            });
        },
        *drawPoint({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: true,
                    mapDraw: {
                        geometryType: 'point',
                        data: {
                            id: `draw${new Date().getTime()}`
                        },
                        parameter: {}
                    }
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: false
                }
            })
        },
        *drawPolyline({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: true,
                    mapDraw: {
                        geometryType: 'polyline',
                        data: {
                            id: `draw${new Date().getTime()}`
                        },
                        parameter: {
                            color: '#ff000f',
                            pellucidity: 0.3,
                            lineWidth: 8,
                            lineType: 'dashed'
                        }
                    }
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: false
                }
            })
        },
        *drawPolygon({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: true,
                    mapDraw: {
                        geometryType: 'polygon',
                        data: {
                            id: `draw${new Date().getTime()}`
                        },
                        parameter: {
                            color: '#fff',
                            lineColor: '#333',
                            lineOpacity: 0.5,
                            pellucidity: 0.7,
                            lineWidth: 10,
                            lineType: 'dashed'
                        }
                    }
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: false
                }
            })
        },
        *drawCircle({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: true,
                    mapDraw: {
                        geometryType: 'circle',
                        data: {
                            id: `draw${new Date().getTime()}`
                        },
                        parameter: {
                            color: '#fff',
                            lineColor: '#333',
                            lineOpacity: 0.5,
                            pellucidity: 0.7,
                            lineWidth: 10,
                            lineType: 'dashed'
                        }
                    }
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: false
                }
            })
        },
        *drawRectangle({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: true,
                    mapDraw: {
                        geometryType: 'rectangle',
                        data: {
                            id: `drawrectangle`
                        },
                        parameter: {
                            color: '#fff',
                            lineColor: '#333',
                            lineOpacity: 0.5,
                            pellucidity: 0.7,
                            lineWidth: 10
                        }
                    }
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: false
                }
            })
        },
        *closeDraw({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    isCloseDraw: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isCloseDraw: false
                }
            })
        },
        *clearAll({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    isClearAll: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isClearAll: false
                }
            })
        },
        *removeGraphic({payload},{select,put,call}){
            let mapRemove = [{
                type: 'point',
                id: 'p00'
            },{
                type: 'draw',
                id: 'drawrectangle'
            }];
            yield put({
                type: 'updateState',
                payload: {
                    mapRemove,
                    isRemove: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isRemove: false
                }
            })
        },
        *openEdit({payload},{select,put,call}){
            let {id} = payload;
            yield put({
                type: 'updateState',
                payload: {
                    editGraphicId: id,
                    isDoEdit: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isDoEdit: false
                }
            })
        },
        *closeEdit({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    isEndEdit: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isEndEdit: false
                }
            })
        },
        *changeAreaRestriction({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    areaRestriction:[[116.455121,40.322595],[119.345746,41.488275]],
                    isSetAreaRestriction: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isSetAreaRestriction: false
                }
            })
        },
        *clearAreaRestriction({payload},{select,put,call}){
            yield put({
                type: 'updateState',
                payload: {
                    isClearAreaRestriction: true
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isClearAreaRestriction: false
                }
            })
        }
    },
    reducers: {
        updateState(state, action) {
            return { ...state, ...action.payload };
        },
        addPoint(state,action){
            let ms = [...state.mapPoints];
            ms.push({
                id: `addpoint${new Date().getTime()}`,
                longitude: 117.468021 + Math.random()*10,
                latitude: 39.890092 + Math.random()*10,
                canShowLabel: true,
                config: {
                    labelContent: 'asdfasdfasdfasfdasf',
                    markerContentX: -13,
                    markerContentY: -42,
                    isAnimation: true
                }
            })
            return {...state,mapPoints: ms};
        },
        updatePoint(state,action){
            let {mapPoints} = state;
            let ms = mapPoints.map((item,index)=>{
                return {
                    ...item,
                    longitude: item.longitude + eval((Math.random()>0.5?'-':'+')+Math.random()*5),
                    latitude: item.latitude + eval((Math.random()>0.5?'-':'+')+Math.random()*5),
                }
            });
            return {...state,mapPoints: ms};
        },
        deletePoint(state,action){
            let {mapPoints} = state;
            let ms = [...mapPoints];
            ms.splice(0,1);
            return {...state,mapPoints: ms};
        },
        addLine(state,action){
            let ms = [...state.mapLines];
            let len = Math.floor(Math.random() * 10) + 1;
            let paths = [];
            for (var i = 0; i <= len; i++) {
                paths.push([
                    Math.random()*100 + 80,
                    Math.random()*30 + 10
                ])
            }
            ms.push({
                id: `line1${new Date().getTime()}`,
                paths: paths,
            })
            return {...state,mapLines: ms};
        },
        updateLine(state,action){
            let {mapLines} = state;
            let ms = mapLines.map((item,index)=>{
                return {
                    ...item,
                    config:{
                        color: '#ff00ff'
                    }
                }
            });
            return {...state,mapLines: ms};
        },
        deleteLine(state,action){
            let {mapLines} = state;
            let ms = [...mapLines];
            ms.splice(0,1);
            return {...state,mapLines: ms};
        },
        addPolygon(state,action){
            let {mapPolygons} = state;
            let ms = [...mapPolygons];
            let len = Math.floor(Math.random() * 10) + 2;
            let rings = [];
            for (var i = 0; i <= len; i++) {
                rings.push([
                    Math.random()*100 + 80,
                    Math.random()*30 + 10
                ])
            }
            ms.push({
                id: `pg${new Date().getTime()}`,
                rings
            })
            return {...state,mapPolygons: ms};
        },
        updatePolygon(state,action){
            let {mapPolygons} = state;
            let ms = [...mapPolygons];
            ms = ms.map((item,index)=>{
                return {
                    ...item,
                    config: {
                        ...item.config,
                        color: '#ffff00',
                        lineColor: '#ff00ff'
                    }
                }
            });
            return {...state,mapPolygons: ms};
        },
        deletePolygon(state,action){
            let {mapPolygons} = state;
            let ms = [...mapPolygons];
            ms.splice(0,1);
            return {...state,mapPolygons: ms};
        },
        addCircle(state,action){
            let {mapCircles} = state;
            let cs = [...mapCircles];
            cs.push({
                id: `circle$${new Date().getTime()}`,
                longitude: Math.random()*100 + 80, 
                latitude: Math.random()*30 + 10,
                radius: 1000000 * Math.random()
            })
            return {...state,mapCircles: cs};
        },
        updateCircle(state,action){
            let {mapCircles} = state;
            let cs = [...mapCircles];
            cs = cs.map((item,index)=>{
                return {
                    ...item,
                    config: {
                        ...item.config,
                        color: '#ffff00',
                        lineColor: '#ff00ff'
                    }
                }
            });
            return {...state,mapCircles: cs};
        },
        deleteCircle(state,action){
            let {mapCircles} = state;
            let cs = [...mapCircles];
            cs.splice(0,1);
            return {...state,mapCircles: cs};
        },
        openTraffic(state,action){
            return{...state,isOpenTrafficInfo: true};
        },
        hideTraffic(state,action){
            return{...state,isOpenTrafficInfo: false};
        },
        addBoundary(state,action){
            let boundaryName = [...state.boundaryName];
            boundaryName.push('无锡');
            return{...state,boundaryName};
        },
        deleteBoundary(state,action){
            let boundaryName = [...state.boundaryName];
            boundaryName.splice(0,1);
            return{...state,boundaryName};
        },
        editPoint(state,action){
            const {id,longitude,latitude} = action.payload;
            let {mapPoints} =state;
            let ms = [...mapPoints];
            ms = ms.map((item)=>{
                if(item.id == id){
                    return {
                        ...item,
                        longitude: longitude,
                        latitude: latitude,
                    };
                }
                return item;
            });
            return {...state,mapPoints: ms};
        },
        editPolygon(state,action){
            const {id,rings} = action.payload;
            let {mapPolygons} =state;
            let ms = [...mapPolygons];
            ms = ms.map((item)=>{
                if(item.id == id){
                    return {
                        ...item,
                        rings: rings
                    }
                }
                return item;
            });
            return {...state,mapPolygons:ms};
        },
        editLine(state,action){
            const {id,paths} = action.payload;
            let {mapLines} =state;
            let ms = [...mapLines];
            ms = ms.map((item)=>{
                if(item.id == id){
                    return {
                        ...item,
                        paths: paths
                    }
                }
                return item;
            });
            return {...state,mapLines: ms};
        },
        editCircle(state,action){
            const {id,longitude,latitude,radius} = action.payload;
            let {mapCircles} =state;
            let ms = [...mapCircles];
            ms = ms.map((item)=>{
                if(item.id == id){
                    return {
                        ...item,
                        longitude,
                        latitude,
                        radius
                    }
                }
                return item;
            });
            return {...state,mapCircles: ms};
        },
        //显示热力图
        showHeatMap(state,action){
            return {...state, heatmap: {
                ...state.heatmap,
                config: {
                    ...state.heatmap.config,
                    visible: true
                }
            }}
        },
        //隐藏热力图
        hideHeatMap(state,action){
            return {...state, heatmap: {
                ...state.heatmap,
                config: {
                    ...state.heatmap.config,
                    visible: false
                }
            }}
        }
    },
}

function delay(timeout) {
    let promise = new Promise((resolve,reject)=>{
        setTimeout(resolve,timeout);
    });
    return promise;
}
